package com.dsl.comp;

import com.dsl.perfs.GroovyShellUtils;

import java.util.Map;
import java.util.Random;
import java.util.TreeMap;


/**
 * https://www.cnblogs.com/mumuxinfei/p/8480179.html
 *
 * @author evoup
 */
public class GroovyPerf {

    @SuppressWarnings("Duplicates")
    public static void main(String[] args) {

        String boolExpr = "1 && 2 || 3 || 4 && (5 || 6)";

        int iterNums = 1000000;
        long randomSeed = 10001L;

        String nboolExpr = boolExpr;
        nboolExpr = nboolExpr.replace("1", "t1");
        nboolExpr = nboolExpr.replace("2", "t2");
        nboolExpr = nboolExpr.replace("3", "t3");
        nboolExpr = nboolExpr.replace("4", "t4");
        nboolExpr = nboolExpr.replace("5", "t5");
        nboolExpr = nboolExpr.replace("6", "t6");

        long beg = System.currentTimeMillis();
        Random random = new Random(randomSeed);
        random.setSeed(randomSeed);
        for ( int i = 0; i <= iterNums; i++) {
            Map<String, Object> params = new TreeMap<>();
            params.put("t1", random.nextBoolean());
            params.put("t2", random.nextBoolean());
            params.put("t3", random.nextBoolean());
            params.put("t4", random.nextBoolean());
            params.put("t5", random.nextBoolean());
            params.put("t6", random.nextBoolean());

            GroovyShellUtils.execExpr(nboolExpr, params, Boolean.class);
        }
        long end = System.currentTimeMillis();
        System.out.println(String.format("total consume: %dms", end - beg));

    }

}
